<?php

class Application_Model_NewsletterMapper
{
    private $content;

    public function send($userMails, $content)
    {
        $this->content = $content;

        $config = array('auth' => 'login', 'username' => 'xxxx', 'password' => 'xxxx');
        $transport = new Zend_Mail_Transport_Smtp('xxx', $config);

        $images = array(
            array('path'=>'/img/newsletter/system/header2.jpg',     'cid'=>'mg1', 'type' => 'image/jpeg'),
            array('path'=>'/img/newsletter/system/slider2.gif',     'cid'=>'mg2', 'type' => 'image/gif'),
            array('path'=>'/img/newsletter/system/separator.jpg',   'cid'=>'mg3', 'type' => 'image/jpeg'),
            array('path'=>'/img/newsletter/system/footer2.jpg',     'cid'=>'mg4', 'type' => 'image/jpeg')
        );

        $counterAll = $counter = 0;
        foreach ($userMails as $to) {

            $counter++;
            $counterAll++;
            if ($counter == 1) {
                $mail = new Zend_Mail('utf-8');
                $mail->setType(Zend_Mime::MULTIPART_RELATED);

                foreach($this->content as $image) {

                    if (strlen($image['img']) > 4) {

                        $img = file_get_contents(APPLICATION_PATH . '/../public/newsletter/' . $image['img']);
                        $at = new Zend_Mime_Part($img);
                        if (preg_match("/..*\.png$/i", $image['img'])) {
                            $at->type = 'image/gif';
                        } elseif (preg_match("/..*\.gif$/i", $image['img'])) {
                            $at->type = 'image/gif';
                        } elseif (preg_match("/..*\.jpg$/", $image['img'])) {
                            $at->type = 'image/jpeg';
                        } else {
                            unset($image['img']);
                        }

                        $at->encoding = Zend_Mime::ENCODING_BASE64;
                        $at->disposition = Zend_Mime::DISPOSITION_INLINE;
                        $at->filename = $image['img']['path'];
                        $at->id = 'image' . $image['id'];

                        $mail->addAttachment($at);
                    }
                }

                foreach($images as $image) {
                    $img = file_get_contents(APPLICATION_PATH . '/../public/' . $image['path']);
                    $at = new Zend_Mime_Part($img);
                    $at->type = $image['type'];
                    $at->encoding = Zend_Mime::ENCODING_BASE64;
                    $at->disposition = Zend_Mime::DISPOSITION_INLINE;
                    $at->filename = $image['path'];
                    $at->id = $image['cid'];
                    $mail->addAttachment($at);
                }

                $mail->setFrom('xxx');
                $mail->setSubject('xxxx / Newsletter');
                $mail->setBodyHtml($this->setHtml());
            } 

            $mail->addBcc($to);
            
            if ($counter == 100) {
                $mail->addTo('kontakt@xxxx');
                $mail->send($transport);
                $counter = 0;
            }
        }
        if ($counter > 0 && $counter < 100) {
            $mail->addTo('kontakt@xxxx');
            $mail->send($transport);
        }

        return $counterAll;
    }

    private function setHtml()
    {
        $text = "
            <!DOCTYPE HTML PUBLIC \"-//W3C//DTD HTML 4.01 Transitional//EN\">
            <html>
            <head>
                <title>xxxx - newsletter</title>
                <meta http-equiv=\"Content-Type\" content=\"text/html; charset=UTF-8\">
            </head>
            <body>
                <table width=\"600\" border=\"0\" align=\"center\" cellpadding=\"0\" cellspacing=\"0\">
                    <tr>
                        <td align=\"center\">
                            <table width=\"600\" border=\"0\" align=\"center\" cellpadding=\"0\" cellspacing=\"0\">
                                <tr>
                                    <td colspan=\"4\"
                                        style=\"text-align: right; font-family: Helvetica;
                                        font-size: 12px; letter-spacing: 1px; line-height: 20px; color: #6b6b6b;\">";
        $dateArray = explode(' ', trim($this->content[0]['data']));
        $text .= $dateArray[0] . " ";
        unset($dateArray[0]);
        $text .= "<span style=\"color: #ea300c\">" . implode(' ', $dateArray) . "</span> &nbsp; &nbsp;
                                    </td>
                                </tr>";
        $text .= "
                                <tr><td colspan=\"4\" align=\"center\"><a href=\"http://www.xxxxx\" target=\"_blank\"><img border=\"0\" alt=\"\" src=\"cid:mg1\"></a></td></tr> ";
        $text .= "
                                <tr><td colspan=\"4\" align=\"center\"><img border=\"0\" alt=\"\" src=\"cid:mg2\"></td></tr>
                                <tr><td colspan=\"4\">&nbsp;<br>&nbsp;<br></td></tr>";
        for ($i = 0; $i < (count($this->content)); $i++) {
            $text .= "
                                <tr>
                                    <td rowspan=\"2\" width=\"10\">&nbsp;</td>
                                    <td rowspan=\"2\" width=\"127\" valign=\"top\">";
            if (isset($this->content[$i]['img'])) {
                $text .= "
                                        <img  width=\"127\" border=\"0\" alt=\"\" src=\"cid:image" . $this->content[$i]['id'] . "\">";
            } else {
                $text .= "
                                        <img  width=\"127\" border=\"0\" alt=\"\" src=\"cid:mg3\">";
            }
            $text .= "
                                    </td>
                                    <td rowspan=\"2\" width=\"19\"><img width=\"19\" border=\"0\" alt=\"\" src=\"cid:mg3\"></td>
                                    <td style=\"text-align: left; vertical-align: top; font-family: Helvetica;
                                        font-size: 17px; font-weight: bold; letter-spacing: 2px; line-height: 20px; color: #000000;\">";
            $titleArray = explode(' ', trim($this->content[$i]['tytul']));
            $text .= "<span style=\"color: #ea300c\">" . $titleArray[0] . "</span> ";
            unset($titleArray[0]);
            $text .= implode(' ', $titleArray) . "<br><br>
                                    </td>
                                </tr>
                                <tr>
                                    <td style=\"text-align: left; vertical-align: top; font-family: Helvetica;
                                        font-size: 11px; letter-spacing: 1px; line-height: 20px; color: #6a6a6a;\">
                                        " . nl2br($this->content[$i]['artykul']) . "
                                    </td>
                                </tr>";
            if (isset($this->content[$i]['href']) && strlen($this->content[$i]['href']) > 10) {
                $text .= "
                                <tr>
                                    <td colspan=\"4\"style=\"text-align: right; font-family: Helvetica;
                                        font-size: 11px; font-weight: bold; letter-spacing: 1px; line-height: 20px; text-decoration: none; \">
                                        <a style=\"color: #ea300c; text-decoration: none; font-weight: bold;\" target=\"_blanc\"
                                        href=\"" . $this->content[$i]['href'] . "\"><span style=\"color: #ea300c; text-decoration: none; font-weight: bold;\">&gt; </span></a>
                                        <a style=\"color: black; text-decoration: none; font-weight: bold;\" target=\"_blanc\"
                                        href=\"" . $this->content[$i]['href'] . "\"><span style=\"color: black; text-decoration: none; font-weight: bold;\">więcej</span></a>
                                        &nbsp; &nbsp;
                                    </td>
                                </tr>";
            }
            $text .= "
                                <tr><td colspan=\"4\">&nbsp;<br>&nbsp;<br>&nbsp;<br></td></tr>";
        }
        $text .= "
                                <tr><td colspan=\"4\" align=\"center\" valign=\"top\"><img border=\"0\" alt=\"\" src=\"cid:mg4\"></td></tr>
                            </table>
                        </td>
                    </tr>
                </table>
            </body>";

        return $text;
    }
}